// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

/**
 * 
 * @author Javen205
 * @version CocosCreator V1.10.0
 * @description 骨骼动画测试 官方API http://docs.cocos.com/creator/api/zh/classes/ArmatureDisplay.html
 * 
 */

cc.Class({
    extends: cc.Component,

    properties: {
        count: {
            get() {
                if (this._count === undefined) {
                    this._count = 0;
                }
                return this._count;
            },
            set(value) {
                this._count = value;
            },
            visible: false,
            tooltip: "计数"
        },
        msg: {
            default: null,
            type: cc.Label,
            tooltip: "提示文字"
        }
    },

    onLoad() {
        //获取 ArmatureDisplay
        this._armatureDisPlay = this.getComponent(dragonBones.ArmatureDisplay);

        //获取 ArmatureDisplay 当前使用的 Armature 对象
        // this._armature = this._armatureDisPlay.armature();
        // cc.log(this._armature);

        // this._animationNames = this._armature.animation._animationNames;
        // cc.log(this._animationNames);

        //获取指定的 armature 的所有动画名称。
        this._animationNames = this._armatureDisPlay.getAnimationNames(this._armatureDisPlay.armatureName);
        cc.log(this._animationNames);
        this.playAnimation(this._animationNames[this.count]);
        //添加动画监听 
        //TODO FADE_IN_COMPLETE有回调其他无
        this._armatureDisPlay.addEventListener(dragonBones.EventObject.FADE_IN_COMPLETE, this.animationEventHandler, this);
        this._armatureDisPlay.addEventListener(dragonBones.EventObject.FADE_OUT_COMPLETE, this.animationEventHandler, this);
        this._armatureDisPlay.addEventListener(dragonBones.EventObject.COMPLETE, this.animationEventHandler, this);
        this._armatureDisPlay.addEventListener(dragonBones.EventObject.FRAME_EVENT, this.animationEventHandler, this);
    },

    playAnimation(animationName) {
        this._armatureDisPlay.playAnimation(animationName, -1);
    },
    setMsg(msg) {
        this.msg.string = msg;
    },
    animationEventHandler(event) {
        cc.log(event);
        if (event.type == dragonBones.EventObject.FADE_IN_COMPLETE) {
            cc.log('FADE_IN_COMPLETE');
        } else if (event.type == dragonBones.EventObject.FADE_OUT_COMPLETE) {
            cc.log('FADE_OUT_COMPLETE');
        } else if (event.type == dragonBones.EventObject.COMPLETE) {
            cc.log('COMPLETE');
        } else if (event.type == dragonBones.EventObject.FRAME_EVENTE) {
            cc.log('FRAME_EVENTE');
        }
    },

    start() {
        let self = this;
        self.node.on('TOUCH_START', function (event) {
            cc.log(event.detail.msg);
            let temp = ++self.count > 3 ? 0 : self.count;
            self.count = temp;
            self.playAnimation(self._animationNames[self.count]);
            if (self.count == 0) {
                self.setMsg("站立");
            } else if (self.count == 1) {
                self.setMsg("行走");
            } else if (self.count == 2) {
                self.setMsg("跳跃");
            } else if (self.count == 3) {
                self.setMsg("跌落");
            }
        });
    },


    // update (dt) {},
});
